package com.bh.shipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.basemapper.UpdateBatchWrapper;
import com.bh.common.constant.Entity;
import com.bh.common.constant.SmsConstants;
import com.bh.common.constant.Symbol;
import com.bh.common.constant.VerifiZbStatus;
import com.bh.common.enums.OrderStatus;
import com.bh.common.enums.ShipmentStatus;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.*;
import com.bh.common.utils.pdf.PdfUtils;
import com.bh.extend.domain.JcAbnormal;
import com.bh.extend.domain.JcAbnormalDetail;
import com.bh.extend.mapper.JcOrderBarcodeMapper;
import com.bh.extend.service.IJcAbnormalDetailService;
import com.bh.extend.service.IJcAbnormalService;
import com.bh.extend.service.IJcOrderBarcodeService;
import com.bh.fee.domain.JcFeeType;
import com.bh.fee.mapper.JcFeeTypeMapper;
import com.bh.framework.manager.AsyncManager;
import com.bh.framework.manager.factory.AsyncFactory;
import com.bh.framework.web.service.PermissionService;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.service.IJcSysOrganizationService;
import com.bh.ledger.domain.JcLedgerDetail;
import com.bh.ledger.domain.JcVerificationZb;
import com.bh.ledger.service.IJcLedgerDetailService;
import com.bh.ledger.service.IJcVerificationZbService;
import com.bh.order.domain.JcInOrOutRecord;
import com.bh.order.domain.JcLed;
import com.bh.order.domain.JcLedReceivingParty;
import com.bh.order.domain.JcOrder;
import com.bh.order.mapper.JcLedMapper;
import com.bh.order.mapper.JcOrderMapper;
import com.bh.order.service.IJcInOrOutRecordService;
import com.bh.order.service.IJcLedReceivingpartyService;
import com.bh.order.service.IJcLedService;
import com.bh.order.service.IJcOrderTrackService;
import com.bh.order.vo.JcLedPrintVO;
import com.bh.order.vo.JcLedVO;
import com.bh.shipment.convert.JcShipmentConvert;
import com.bh.shipment.domain.JcBerthStand;
import com.bh.shipment.domain.JcShipment;
import com.bh.shipment.domain.JcShipmentImg;
import com.bh.shipment.domain.JcShipmentTrack;
import com.bh.shipment.mapper.JcBerthStandMapper;
import com.bh.shipment.mapper.JcShipmentMapper;
import com.bh.shipment.service.IJcBerthStandService;
import com.bh.shipment.service.IJcShipmentImgService;
import com.bh.shipment.service.IJcShipmentService;
import com.bh.shipment.service.IJcShipmentTrackService;
import com.bh.shipment.vo.*;
import com.bh.vehicle.domain.JcCarrier;
import com.bh.vehicle.mapper.JcCarrierMapper;
import com.bh.vehicle.service.IJcCarrierService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 运单管理Service业务层处理
 *
 * @author slx
 * @date 2023-04-22
 */

@Service
public class JcShipmentServiceImpl extends ServiceImpl<JcShipmentMapper, JcShipment> implements IJcShipmentService {
    @Autowired
    private JcShipmentMapper jcShipmentMapper;
    @Autowired
    private JcOrderBarcodeMapper jcOrderBarcodeMapper;
    @Autowired
    private IJcOrderBarcodeService jcOrderBarcodeService;
    @Autowired
    private JcLedMapper jcLedMapper;
    @Autowired
    private IJcLedService jcLedService;
    @Autowired
    private IJcLedReceivingpartyService jcLedReceivingPartyService;
    @Autowired
    private JcOrderMapper jcOrderMapper;
    @Autowired
    private IJcOrderTrackService jcOrderTrackService;
    @Autowired
    private IJcLedReceivingpartyService jcLedReceivingpartyService;
    @Autowired
    private IJcShipmentTrackService jcShipmentTrackService;
    @Autowired
    private IJcCarrierService carrierService;
    @Autowired
    private IJcBerthStandService jcBerthStandService;
    @Autowired
    private IJcSysOrganizationService sysOrganizationService;
    @Autowired
    private IJcLedgerDetailService jcLedgerDetailService;
    @Autowired
    private IJcInOrOutRecordService jcInOrOutRecordService;
    @Autowired
    private JcBerthStandMapper jcBerthStandMapper;
    @Autowired
    private IJcShipmentImgService jcShipmentImgService;
    @Autowired
    private IJcAbnormalService abnormalService;
    @Autowired
    private IJcAbnormalDetailService abnormalDetailService;
    @Autowired
    private JcFeeTypeMapper feeTypeMapper;
    @Autowired
    private JcCarrierMapper carrierDao;
    @Resource
    private RedisCodeUtil redisCodeUtil;
    @Autowired
    private JcShipmentConvert jcShipmentConvert;
    @Autowired
    protected PermissionService permissionService;
    @Autowired
    protected IJcVerificationZbService jcVerificationZbService;

    /**
     * 查询运单管理
     *
     * @param id 运单管理主键
     * @return 运单管理
     */
    @Override
    public JcShipment selectJcShipmentById(Long id) {
        JcShipment shipment = jcShipmentMapper.selectById(id);
        List<JcLed> leds = jcLedMapper.selectJcLedListByShipmentId(id);
        if (leds.size() > 0) {
            shipment.setLeds(leds);
        }
        if (shipment.getJcFromOrganizationId() != null) {
            JcSysOrganization organization = sysOrganizationService.selectJcSysOrganizationById(shipment.getJcFromOrganizationId());
            shipment.setFromOrganization(organization);
        }
        if (shipment.getJcNextOrganizationId() != null) {
            JcSysOrganization organization = sysOrganizationService.selectJcSysOrganizationById(shipment.getJcNextOrganizationId());
            shipment.setNextOrganization(organization);
        }
        if (shipment.getJcToOrganizationId() != null) {
            JcSysOrganization organization = sysOrganizationService.selectJcSysOrganizationById(shipment.getJcToOrganizationId());
            shipment.setToOrganization(organization);
        }
        List<JcShipmentImg> imgs = jcShipmentImgService.selectJcShipmentImgByShipmentId(id);
        if (imgs != null && !imgs.isEmpty()) {
            shipment.setImgs(imgs);
            List<String> imgUrls = imgs.stream().map(JcShipmentImg::getImgUrl).collect(Collectors.toList());
            shipment.setImgUrls(StringUtils.join(imgUrls, ","));
        }

        // 查询台帐明细
        if (permissionService.hasRole("financial") || permissionService.hasRole("admin")) {
            List<JcLedgerDetail> ledgerDetails = jcLedgerDetailService.selectJcLedgerDetailListByShipmentId(id);
            if (ledgerDetails != null && !ledgerDetails.isEmpty()) {
                shipment.setLedgerDetails(ledgerDetails);
            }
        }

        List<JcBerthStand> berthStands = jcBerthStandMapper.selectJcBerthStandListByShipmentId(id);
        if (berthStands != null && !berthStands.isEmpty()) {
            shipment.setBerthStands(berthStands);
        }
        return shipment;
    }

    @Override
    public JcShipment selectJcShipmentByCode(String code) {
        JcShipment shipment = jcShipmentMapper.selectJcShipmentByCode(code);
        if (shipment != null) {
            JcSysOrganization formOrganization = sysOrganizationService.selectJcSysOrganizationById(shipment.getJcFromOrganizationId());
            shipment.setFromOrganization(formOrganization);

            JcSysOrganization toOrganization = sysOrganizationService.selectJcSysOrganizationById(shipment.getJcToOrganizationId());
            shipment.setToOrganization(toOrganization);

            List<JcShipmentImg> imgs = jcShipmentImgService.selectJcShipmentImgByShipmentId(shipment.getId());
            if (imgs != null && !imgs.isEmpty()) {
                shipment.setImgs(imgs);
                List<String> imgUrls = imgs.stream().map(JcShipmentImg::getImgUrl).collect(Collectors.toList());
                shipment.setImgUrls(StringUtils.join(imgUrls, ","));
            }
        }
        return shipment;
    }

    @DataDictClass
    @Override
    public JcShipmentUnloadVo selectJcShipmentUnloadVoByCode(String code) {
        JcShipment shipment = jcShipmentMapper.selectJcShipmentByCode(code);
        JcShipmentUnloadVo unloadVo = jcShipmentConvert.toUnloadVo(shipment);
        if (shipment != null && unloadVo != null) {
            List<JcShipmentImg> imgs = jcShipmentImgService.selectJcShipmentImgByShipmentId(shipment.getId());
            if (imgs != null && imgs.size() > 0) {
                List<String> imgUrls = imgs.stream().map(JcShipmentImg::getImgUrl).collect(Collectors.toList());
                unloadVo.setImgUrls(StringUtils.join(imgUrls, ","));
            }
            List<String> ledIds = jcOrderBarcodeMapper.selectUnloadLedIdsByShipmentId(shipment.getId(), shipment.getJcNowOrganizationId());
            if (ledIds != null && ledIds.size() > 0) {
                unloadVo.setTotalOrderNum(ledIds.size());
                unloadVo.setNumber(jcOrderBarcodeMapper.selectUnloadLedNumByShipmentId(shipment.getId(), shipment.getJcNowOrganizationId()));
            }
        }
        return unloadVo;
    }

    /**
     * 查询运单管理列表
     *
     * @param jcShipment 运单管理
     * @return 运单管理
     */
    @DataDictClass
    @Override
    public List<JcShipmentSysApiVO> selectJcShipmentSysList(JcShipmentSysVo jcShipment) {
        List<JcShipment> list = jcShipmentMapper.selectJcShipmentSysList(jcShipment);
        return jcShipmentConvert.toSysApiVo(list);
    }


    @DataDictClass
    @Override
    public PageInfo<JcShipmentListVo> selectPageJcShipmentList(JcShipmentSysVo jcShipment) {
        PageUtils.startPage();
        List<JcShipment> shipments = jcShipmentMapper.selectJcShipmentSysList(jcShipment);
        // 3. 封装list到PageInfo对象中自动分页
        PageInfo<JcShipment> pageInfo = new PageInfo<>(shipments);
        // 计算总费用
        List<JcShipmentListVo> listVos = jcShipmentConvert.toListVo(pageInfo.getList());
        for (JcShipmentListVo shipment : listVos) {
            List<JcLedgerDetail> ledgerDetails = jcLedgerDetailService.selectJcLedgerDetailListByShipmentId(shipment.getId());
            BigDecimal total = BigDecimal.ZERO;
            for (JcLedgerDetail ledgerDetail : ledgerDetails) {
                total = total.add(ledgerDetail.getAmount());
            }
            shipment.setTotalFee(total);
        }

        PageInfo<JcShipmentListVo> pageInfoVo = new PageInfo<>();
        pageInfoVo.setTotal(pageInfo.getTotal());
        pageInfoVo.setList(listVos);
        return pageInfoVo;
    }


    @DataDictClass
    @Override
    public List<JcShipmentListVo> selectJcShipmentList(JcShipmentSysVo jcShipment) {
        List<JcShipment> shipments = jcShipmentMapper.selectJcShipmentSysList(jcShipment);
        // 计算总费用
        for (JcShipment shipment : shipments) {
            List<JcLedgerDetail> ledgerDetails = jcLedgerDetailService.selectJcLedgerDetailListByShipmentId(shipment.getId());
            BigDecimal total = BigDecimal.ZERO;
            for (JcLedgerDetail ledgerDetail : ledgerDetails) {
                total = total.add(ledgerDetail.getAmount());
            }
            shipment.setTotalFee(total);
        }
        return jcShipmentConvert.toListVo(shipments);
    }


    @DataDictClass
    @Override
    public List<JcShipmentListVo> selectTrackShipmentList(JcShipmentSysVo sysVo) {
        List<JcShipment> shipments = jcShipmentMapper.selectTrackShipmentList(sysVo);
        return jcShipmentConvert.toListVo(shipments);
    }

    /**
     * 专门为别的机构查看到站车辆
     *
     * @param
     * @return
     */
    @DataDictClass
    @Override
    public List<JcShipmentListVo> getRVShipmentList(JcShipmentSysVo sysVo) {
        List<JcShipment> shipments = jcShipmentMapper.getRVShipmentList(sysVo);
        return jcShipmentConvert.toListVo(shipments);
    }

    /**
     * 获取 到站记录
     *
     * @param jcShipment 运单管理
     * @return
     */
    @DataDictClass
    @Override
    public List<JcShipmentListVo> getDZShipmentInfo(JcShipmentSysVo jcShipment) {
        List<JcShipment> shipments = jcShipmentMapper.getDZShipmentInfo(jcShipment);
        return jcShipmentConvert.toListVo(shipments);
    }

    /**
     * 获取 到站记录
     */
    @DataDictClass
    @Override
    public List<JcShipmentListVo> getDZShipmentList(JcShipmentSysVo jcShipment) {
        List<JcShipment> list = jcShipmentMapper.getDZShipmentList(jcShipment);
        return jcShipmentConvert.toListVo(list);
    }


    @DataDictClass
    @Override
    public List<JcShipmentSysApiVO> selectDriverJcShipmentList(JcShipmentSysVo jcShipment) {
        List<JcShipment> list = jcShipmentMapper.selectDriverJcShipmentList(jcShipment);
        return jcShipmentConvert.toSysApiVo(list);
    }

    /**
     * 新增运单管理
     *
     * @param shipment 运单管理
     * @return
     */
    @Override
    public int insertOrUpdateJcShipment(JcShipment shipment) {
        JcShipment oldShipment = null;
        //绑定组织机构
        shipment.setJcOrganizationId(SecurityUtils.getOrgId());
        if (StringUtils.isEmpty(shipment.getId())) {
            // 如果是空 说明是新建设置
            shipment.setCode(redisCodeUtil.getShipmentCode());
            shipment.setCreateTime(DateUtils.getNowDate());
            shipment.setCreateName(SecurityUtils.getUsername());
        } else {
            //查出原始数据
            oldShipment = jcShipmentMapper.selectById(shipment.getId());
        }
        //处理合同是否超期
        if (shipment.getJcCarrierId() != null) {
            JcCarrier carrier = carrierService.getById(shipment.getJcCarrierId());
            Date startTime = carrier.getStartTime();
            Date endTime = carrier.getEndTime();
            Date timestamp = shipment.getTime();
            boolean b = DateUtils.belongCalendar(timestamp, startTime, endTime);
            if (b) {
                shipment.setCarriageIsExceed(Entity.UNACTIVE);//状态0 超期
            } else {
                shipment.setCarriageIsExceed(Entity.ACTIVE);// 状态1 不超期
            }
            shipment.setCarrierType(carrier.getCarrierType());
        } else {
            shipment.setCarriageIsExceed(0);//临时承运商，不计算是否超期
            shipment.setCarrierType(0);
        }
        //处理关联信息
        shipment.setRefuse(0);
        shipment.setStatus(ShipmentStatus.ACTIVE.getValue());
        shipment.setJcNowOrganizationId(shipment.getJcFromOrganizationId());
        shipment.setCarrierType(shipment.getCarrierType());
        List<JcLedgerDetail> ledgerDetails = shipment.getLedgerDetails();//获取台账明细
        shipment.setLedgerDetails(null);//阻断关系
        int num;
        if (oldShipment != null) {
            shipment.setOrderCode(oldShipment.getOrderCode());
            shipment.setRelateBill1(oldShipment.getRelateBill1());
            num = jcShipmentMapper.updateById(shipment);
        } else {
            num = jcShipmentMapper.insert(shipment);
        }
        // 更新的时候需要重新删除
        jcBerthStandService.deleteJcBerthStandByShipmentId(shipment.getId());
        //处理经由站
        String orgIds = shipment.getOrgIds();
        List<Long> sp = new ArrayList<>();  // 站点不一致验证
        sp.add(shipment.getJcFromOrganizationId());
        sp.add(shipment.getJcToOrganizationId());
        if (StringUtils.isNotEmpty(orgIds)) {
            String[] orgIdArrays = orgIds.split(Symbol.COMMA);
            shipment.setBerthStand(null);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < orgIdArrays.length; i++) {
                String s = orgIdArrays[i];
                sp.add(Long.valueOf(s));
                JcSysOrganization org = sysOrganizationService.selectJcSysOrganizationById(Long.valueOf(s));
                int type = org.getType();
                if (type == 3) {
                    throw new ServiceException("途径站不能是落地派机构");
                }
                JcBerthStand berthStand = new JcBerthStand();
                berthStand.setJcOrganizationId(org.getId());
                berthStand.setJcShipmentId(shipment.getId());
                berthStand.setIsArrive(0);
                berthStand.setOrderBy(i);
                berthStand.setStatus(Entity.UNACTIVE);
                sb.append(org.getName()).append("-");
                jcBerthStandService.insertJcBerthStand(berthStand);
            }
            sb.deleteCharAt(sb.lastIndexOf("-"));
            shipment.setOrgnameList(sb.toString());
            jcShipmentMapper.updateById(shipment);
        }
        Set<Long> set = new HashSet<>(sp);
        if (set.size() != sp.size()) {
            throw new ServiceException("线路站点不能重复");
        }
        //处理运单跟踪
        if (oldShipment == null) {
            // 如果是空 说明是新建设置
            jcShipmentTrackService.addTrack(shipment.getId(), "运单已经被保存 运单号:" + shipment.getCode());
        } else {
            jcShipmentTrackService.addTrack(shipment.getId(), "运单已经被修改");
        }

        // 处理费用相关
        jcLedgerDetailService.deleteByShipmentId(shipment.getId());
        if (ledgerDetails != null && ledgerDetails.size() > 0) {
            for (JcLedgerDetail ledgerDetail : ledgerDetails) {
                ledgerDetail.setJcShipmentId(shipment.getId());
                ledgerDetail.setType(1);
                ledgerDetail.setCategory(1);
                ledgerDetail.setStatus(Entity.ACTIVE);
                ledgerDetail.setIsConfirm(0);
                ledgerDetail.setCreateName(SecurityUtils.getUsername());
                ledgerDetail.setCreateTime(DateUtils.getNowDate());
            }
            jcLedgerDetailService.saveBatch(ledgerDetails);
        }
        return num;
    }


    @Override
    public int saveAbnormalShipment(JcShipmentAbnormalVO abnormalVO) {
        JcShipment newShipment = abnormalVO.getJcShipment();

        //查出原始数据
        JcShipment oldShipment = selectJcShipmentById(newShipment.getId());
        if (oldShipment.getStatus() == ShipmentStatus.UNACTIVE.getValue()
                || oldShipment.getStatus() == ShipmentStatus.ACTIVE.getValue()) {
            throw new ServiceException("不能异常修改");
        }
        if (newShipment.getOperationPattern() == 1) {
            if (newShipment.getStatus() == 4) {
                oldShipment.setStatus(ShipmentStatus.ARRIVE.getValue());
                List<JcLed> leds = oldShipment.getLeds();
                for (JcLed led : leds) {
                    JcOrder order = jcOrderMapper.selectById(led.getJcOrderId());
                    order.setStatus(ShipmentStatus.ARRIVE.getValue());
                }
            }
        }
        JcAbnormal abnormal = new JcAbnormal();
        abnormal.setJcOrganizationId(SecurityUtils.getOrgId());
        List<JcAbnormalDetail> abnormalList = abnormalDetailService.getAbnormalList(oldShipment, newShipment);
        if (newShipment.getStatus() == ShipmentStatus.CONFIGRM.getValue() || newShipment.getStatus() == ShipmentStatus.DESPATCH.getValue()) {
            if (!oldShipment.getToOrganization().equals(newShipment.getToOrganization())) {
                JcSysOrganization oldToOrg = sysOrganizationService.getById(oldShipment.getJcToOrganizationId());
                JcSysOrganization newToOrg = sysOrganizationService.getById(newShipment.getJcToOrganizationId());
                abnormalList.add(getAbnormalDetail("jcToOrganizationId", oldToOrg.getName(), "目的网点", newToOrg.getName()));
                oldShipment.setToOrganization(newShipment.getToOrganization());
                if (oldShipment.getNowOrganization().getId().equals(oldShipment.getToOrganization().getId())) {
                    oldShipment.setNextOrganization(null);
                } else {
                    oldShipment.setNextOrganization(newShipment.getToOrganization());
                }
            }

        }

        //处理 核销记录对象
        List<JcVerificationZb> verifications = jcVerificationZbService.selectJcVerificationZbByShipmentId(newShipment.getId());//获得核销总表
        List<JcLedgerDetail> ledgerDetails = newShipment.getLedgerDetails();

        for (JcLedgerDetail ledgerDetail : ledgerDetails) {
            BigDecimal amount = ledgerDetail.getAmount();
            BigDecimal oilPrepaid = ledgerDetail.getOilPrepaid() == null ? BigDecimal.ZERO : ledgerDetail.getOilPrepaid();//新预付油卡
            BigDecimal cashAdvances = ledgerDetail.getCashAdvances() == null ? BigDecimal.ZERO : ledgerDetail.getCashAdvances();//新预付现金
            BigDecimal input = ledgerDetail.getInput() == null ? BigDecimal.ZERO : ledgerDetail.getInput();
            BigDecimal taxRate = ledgerDetail.getTaxrate() == null ? BigDecimal.ZERO : ledgerDetail.getTaxrate();
            if (amount.compareTo(oilPrepaid.add(cashAdvances)) < 0 && amount.compareTo(BigDecimal.ZERO) >= 0) {
                throw new ServiceException("预付油卡和预付现金，加一起不能超过费用金额");
            }
            JcFeeType feeType = feeTypeMapper.selectById(ledgerDetail.getJcFeeTypeId());
            JcCarrier carrier = carrierDao.selectById(ledgerDetail.getJcCarrierId());
            if (StringUtils.isEmpty(ledgerDetail.getId())) {
                abnormalList.add(getAbnormalDetail("调整费用-" + feeType.getName(), "0", "调整费用-" + feeType.getName() + "  税率:" + ledgerDetail.getTaxrate(), ledgerDetail.getAmount().toString()));
                abnormalList.add(getAbnormalDetail("承运商", "无", "承运商", carrier.getName()));
//                ledgerDetail.setJcShipmentId(oldShipment.getJcCarrierId());
                JcVerificationZb verificationZb = verifications.get(0);
                verificationZb.setWhxMoney(verificationZb.getWhxMoney().add(ledgerDetail.getAmount()));
                verificationZb.setZmoney(verificationZb.getZmoney().add(ledgerDetail.getAmount()));
                if (verificationZb.getStatus() == VerifiZbStatus.ACTIVE) {//核销完毕
                    verificationZb.setStatus(VerifiZbStatus.PART_VER);//变成部分核销
                }
                jcVerificationZbService.updateJcVerificationZb(verificationZb);
            }
        }
        jcLedgerDetailService.updateBatchById(ledgerDetails);


        if (abnormalList.isEmpty()) {
            throw new ServiceException("没有异常，不用修改");
        } else {
            abnormal.setDescription(abnormalVO.getAbnormal());
            abnormal.setNumber(abnormalList.size());
            abnormal.setJcShipmentId(oldShipment.getId());
            abnormal.setTime(DateUtils.getNowDate());
            abnormal.setType(1);
            abnormalService.insertJcAbnormal(abnormal);
            for (JcAbnormalDetail abnormalDetail : abnormalList) {
                abnormalDetail.setJcAbnormalId(abnormal.getId());
            }
            abnormalDetailService.saveBatch(abnormalList);
        }
        newShipment.setIsAbnormal(1);

        return jcShipmentMapper.updateById(newShipment);
    }

    @Override
    public int arriveJcShipmentList(JcShipment jcShipment) {
        return jcShipmentMapper.arriveJcShipmentList(jcShipment);
    }

    /**
     * @param id
     * @param source driver 代表司机发运， org 代表卸货完成发运
     * @return
     */
    @Override
    public int updateRegTime(Long id, String source) {
        Date factLeaverTime = new Date();
        JcShipment shipment = getById(id);
        //运作模式为 自营车辆 的情况下，需要判断是否到达终点站
        if (shipment.getOperationPattern() == 0) {
            if (shipment.getJcNowOrganizationId().equals(shipment.getJcToOrganizationId())) {
                throw new ServiceException("该票运单已经到达终点站，不能继续操作发运");
            }
            int status = shipment.getStatus();
            if (status == 0 || status == 1) {
                throw new ServiceException("运单状态异常 不能发运");
            }
        }
        shipment.setStatus(ShipmentStatus.DESPATCH.getValue());
        shipment.setFactLeaverTime(factLeaverTime);
        shipment.setModifyName(SecurityUtils.getUsername());
        shipment.setModifyTime(DateUtils.getNowDate());
        List<JcBerthStand> berthStands = jcBerthStandMapper.selectJcBerthStandListByShipmentId(id);
        if (berthStands == null || berthStands.size() == 0) {
            shipment.setJcNextOrganizationId(shipment.getJcToOrganizationId());
        } else {
            boolean panding = true;
            for (JcBerthStand berthStand : berthStands) {
                if (berthStand.getIsArrive() == 0) {
                    // 需要判断是否有货物存在，否则直接到下一个点
                    Integer number = jcLedMapper.selectJcLedListByOrgId(berthStand.getJcOrganizationId(), id);
                    if (number > 0) {
                        shipment.setJcNextOrganizationId(berthStand.getJcOrganizationId());
                        panding = false;
                        break;
                    }
                }
            }
            if (panding) {
                shipment.setJcNextOrganizationId(shipment.getJcToOrganizationId());
            }
        }

        JcSysOrganization toOrg = sysOrganizationService.getById(shipment.getJcToOrganizationId());//获取下一站
        JcSysOrganization nextOrg = sysOrganizationService.getById(shipment.getJcNextOrganizationId());//获取下一站

        List<JcShipmentImg> imgs = jcShipmentImgService.selectJcShipmentImgByShipmentId(id);
        if (imgs != null && imgs.size() > 0) {
            List<String> imgUrls = imgs.stream().map(JcShipmentImg::getImgUrl).collect(Collectors.toList());
            jcShipmentTrackService.addTrack(id, "运单已发运 正在前往下一站：" + nextOrg.getName(), StringUtils.join(imgUrls, ","));
        } else {
            jcShipmentTrackService.addTrack(id, "运单已发运 正在前往下一站：" + nextOrg.getName());
        }

        //
        if ("driver".equals(source)) {
            List<JcLed> leds = jcLedMapper.selectJcLedListByShipmentId(id);
            List<JcOrder> jcOrders = new ArrayList<>();
            List<JcInOrOutRecord> jcInOrOutRecords = new ArrayList<>();
            for (JcLed led : leds) {
                led.setStatus(OrderStatus.DESPATCH.getValue()); //修改分段运单状态
                led.setFactLeaveTime(factLeaverTime);
                JcOrder jcOrder = jcOrderMapper.selectById(led.getJcOrderId());
                //是否拆单
                if (jcOrder.getIsTake() == 1) {
                    jcOrderTrackService.addTrack(led.getJcOrderId(), "您的订单已被拆分:拆分单号" + led.getCode() + " 已发往目的站:" + toOrg.getName() + "  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                } else {
                    jcOrderTrackService.addTrack(led.getJcOrderId(), "已发往下一站:" + nextOrg.getName() + "  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                }
                // 目的是为了更新对应订单状态
                jcOrder.setStatus(OrderStatus.DESPATCH.getValue());
                jcOrder.setFactLeaveTime(factLeaverTime);
                jcOrders.add(jcOrder);

                JcInOrOutRecord inOrOutRecord = new JcInOrOutRecord();
                inOrOutRecord.setTime(factLeaverTime);
                inOrOutRecord.setJcOrganizationId(SecurityUtils.getOrgId());
                inOrOutRecord.setWeight(led.getWeight());
                inOrOutRecord.setVolume(led.getVolume());
                inOrOutRecord.setNumber(led.getNumber());
                inOrOutRecord.setType(1);
                inOrOutRecord.setJcLedId(led.getId());
                inOrOutRecord.setJcZoneStoreroomId(jcOrder.getJcZoneStoreroomId());
                inOrOutRecord.setCreateName(SecurityUtils.getUsername());
                inOrOutRecord.setCreateTime(DateUtils.getNowDate());
                jcInOrOutRecords.add(inOrOutRecord);
            }
            jcLedMapper.updateBatch(leds, new UpdateBatchWrapper<JcLed>().setUpdateFields(
                    JcLed::getStatus,
                    JcLed::getFactLeaveTime
            ));
            if (!jcOrders.isEmpty()) {
                jcOrderMapper.updateBatch(jcOrders, new UpdateBatchWrapper<JcOrder>().setUpdateFields(
                        JcOrder::getStatus,
                        JcOrder::getFactLeaveTime
                ));
            }
            if (!jcInOrOutRecords.isEmpty()) {
                jcInOrOutRecordService.saveBatch(jcInOrOutRecords);
            }
        }
        return jcShipmentMapper.updateById(shipment);
    }

    /**
     * 操作运抵-司机
     *
     * @param id
     * @param date
     * @return
     */
    @Override
    public int updateArrivedTime(Long id, Date date) {
        JcShipment shipment = selectJcShipmentById(id);//修改运单状态
        if (shipment.getOperationPattern() == 0 && shipment.getStatus() != OrderStatus.DESPATCH.getValue()) {
            // 运作模式是自营
            throw new ServiceException("运单不是发运状态，不能操作运抵");
        }
        shipment.setStatus(ShipmentStatus.ARRIVE.getValue());
        shipment.setFactArriveTime(date);
        List<JcLed> leds = shipment.getLeds();
        List<JcOrder> jcOrders = new ArrayList<>();
        for (JcLed led : leds) {
            led.setStatus(OrderStatus.ARRIVE.getValue()); //修改分段运单状态
            led.setFactArriveTime(date);
            JcOrder jcOrder = jcOrderMapper.selectById(led.getJcOrderId());
            //是否拆单
            if (jcOrder.getIsTake() == 1) {
                jcOrderTrackService.addTrack(led.getJcOrderId(), "您的订单已被拆分:拆分单号" + led.getCode() + " 已运抵目的站:" + shipment.getToOrganization().getName() + "  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
            } else {
                jcOrderTrackService.addTrack(led.getJcOrderId(), "已运抵目的站:" + shipment.getToOrganization().getName() + "  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
            }
            //同步修改order 状态
            jcOrder.setStatus(OrderStatus.ARRIVE.getValue());
            jcOrder.setFactArriveTime(date);
            jcOrders.add(jcOrder);
            jcLedMapper.updateById(led);
        }
        if (!jcOrders.isEmpty()) {
            jcOrderMapper.updateBatch(jcOrders, new UpdateBatchWrapper<JcOrder>().setUpdateFields(
                    JcOrder::getStatus,
                    JcOrder::getFactArriveTime
            ));
        }

        jcShipmentTrackService.addTrack(id, "运单已经运抵" + shipment.getToOrganization().getName());
        if (shipment.getJcNextOrganizationId().equals(shipment.getJcToOrganizationId())) {
            // 如到达终点则必须先更改下一站的问题
            shipment.setJcNowOrganizationId(shipment.getJcToOrganizationId());
            shipment.setJcNextOrganizationId(null);
        }
        return updateById(shipment) ? 1 : 0;
    }

    /**
     * 有错误！！！！！-已经不使用
     *
     * @param addShipmentFinishedVo
     * @return
     */
    @Deprecated
    @Override
    public int updateTransportFinished(JcShipmentFinishedVo addShipmentFinishedVo) {
        //修改运单状态
        JcShipment shipment = selectJcShipmentById(addShipmentFinishedVo.getJcShipmentId());
        if (shipment.getOperationPattern() == 0) {
            // 运作模式是自营
            int status = shipment.getStatus();
            if (status != OrderStatus.ARRIVE.getValue()) {
                throw new ServiceException("运单不是运状态，不能操作为完成运输");
            }
        }
        Set<Long> orderIds = new HashSet<>();
        List<JcLed> leds = shipment.getLeds();
        for (JcLed led : leds) {
            if (led.getStatus().equals(OrderStatus.DESPATCH.getValue()) || led.getStatus().equals(OrderStatus.ARRIVE.getValue())) {
                led.setStatus(OrderStatus.TRANSPORT_FINISHED.getValue()); //修改分段运单状态
                //同步修改order 状态
                JcOrder jcOrder = jcOrderMapper.selectById(led.getJcOrderId());
                //是否拆单
                if (jcOrder.getIsTake() == 1) {
                    jcOrderTrackService.addTrack(led.getJcOrderId(), "您的订单已被拆分:拆分单号" + led.getCode() + " 已在目的站:" + shipment.getToOrganization().getName() + "验收确认  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                } else {
                    jcOrderTrackService.addTrack(led.getJcOrderId(), "已在目的站:" + shipment.getNextOrganization().getName() + "验收确认  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                }
                //同步修改order 状态
                orderIds.add(led.getJcOrderId());
                // 发送短信,物流状态通知，查询收货人
                // 尊敬的${name}，您的物流号为：${code}的货物已到达${place}，请及时联系网点管理员获取更多配送信息，联系电话${phone}
                Map<String, String> map = new HashMap<>();
                JcLedReceivingParty receivingParty = jcLedReceivingpartyService.selectJcLedReceivingPartyListByLedIdAndType(led.getId(), "1");
                map.put("name", receivingParty.getContactperson());
                map.put("phone", receivingParty.getIphone());
                map.put("code", led.getCode());
                map.put("place", shipment.getNextOrganization().getName());
                AsyncManager.me().execute(AsyncFactory.pushSms(SecurityUtils.getSysUser().getPhonenumber(), map, SmsConstants.CACHE_TEMPLATE_CODE_3));

                jcLedMapper.updateById(led);
            }
        }
        //同步修改order 状态
        if (orderIds.size() > 0) {
            List<String> transportFinishedIds = jcLedMapper.selectTransportFinishedJcLedListByOrderIds(orderIds);
            if (transportFinishedIds != null && transportFinishedIds.size() > 0) {
                List<JcOrder> jcOrders = jcOrderMapper.selectBatchIds(transportFinishedIds);
                for (JcOrder jcOrder : jcOrders) {
                    jcOrder.setStatus(OrderStatus.TRANSPORT_FINISHED.getValue());
                }
                jcOrderMapper.updateBatch(jcOrders, new UpdateBatchWrapper<JcOrder>().setUpdateFields(
                        JcOrder::getStatus
                ));
            }
        }
        if (shipment.getNumber().equals(addShipmentFinishedVo.getTotalNum())) {
            jcShipmentTrackService.addTrack(shipment.getId(), "运单已在目的站" + shipment.getToOrganization().getName() + "验收确认!");
            shipment.setStatus(ShipmentStatus.TRANSPORT_FINISHED.getValue());
            return updateById(shipment) ? 1 : 0;
        }
        return 0;

    }

    /**
     * 卸货完成-网点管理员.卸货并保存才是真正的卸货完成，否则只是卸货，小程序状态依然会显示未卸货
     *
     * @param shipmentId
     * @return
     */
    @Override
    public int unloadFinished(Long shipmentId) {
        JcShipment shipment = selectJcShipmentById(shipmentId);
        // 因为没有提前到站，因此只能用next,正常逻辑需要用now
        int num = 0;
        List<String> ledIds = jcOrderBarcodeMapper.selectNoSaveUnloadLedIdsByShipmentIdAndOrgId(shipmentId, shipment.getJcNowOrganizationId());
        if (ledIds != null && !ledIds.isEmpty()) {
            //先获取已经装载的 分段订单（LED）
            Map<String, Map<String, Long>> ledOrders = jcOrderBarcodeMapper.selectNoSaveUnloadLedOrderIdsByShipmentId(shipmentId, StringUtils.join(ledIds, ","));

            Set<Long> orderIds = new HashSet<>();
            for (String ledId : ledIds) {
                JcLed led = jcLedMapper.selectById(ledId);
                boolean panding0 = led.getStatus().equals(OrderStatus.DESPATCH.getValue()) || led.getStatus().equals(OrderStatus.ARRIVE.getValue());
                boolean panding1 = led.getNumber() == ledOrders.get(led.getId()).get("scan_number").intValue();
                if (panding0 && panding1) {
                    led.setStatus(OrderStatus.TRANSPORT_FINISHED.getValue()); //修改分段运单状态
                    num += jcLedMapper.updateById(led);
                    //同步修改order 状态
                    JcOrder jcOrder = jcOrderMapper.selectById(led.getJcOrderId());
                    JcSysOrganization organization = sysOrganizationService.getById(jcOrder.getJcToOrganizationId());
                    if (jcOrder.getIsTake() == 1) {
                        jcOrderTrackService.addTrack(led.getJcOrderId(), "您的拆分单号为：" + led.getCode() + " 已在目的站:" + organization.getName() + "验收确认  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                    } else {
                        jcOrderTrackService.addTrack(led.getJcOrderId(), "已在目的站:" + organization.getName() + "验收确认  货品 数量:" + led.getNumber() + " 重量:" + led.getWeight() + " 体积:" + led.getVolume());
                    }
                    //同步修改order 状态
                    orderIds.add(led.getJcOrderId());
                }
            }
            //同步修改order 状态
            if (!orderIds.isEmpty()) {
                List<String> transportFinishedIds = jcLedMapper.selectTransportFinishedJcLedListByOrderIds(orderIds);
                if (transportFinishedIds != null && !transportFinishedIds.isEmpty()) {
                    List<JcOrder> jcOrders = jcOrderMapper.selectBatchIds(transportFinishedIds);
                    for (JcOrder jcOrder : jcOrders) {
                        jcOrder.setStatus(OrderStatus.TRANSPORT_FINISHED.getValue());
                    }
                    jcOrderMapper.updateBatch(jcOrders, new UpdateBatchWrapper<JcOrder>().setUpdateFields(
                            JcOrder::getStatus
                    ));
                }
            }
        }
        // 必须是订单在该站点完成所有卸货才能再次发运运单,否则必须司机进行手动发运进行检测是否完成卸货
        if (!shipment.getJcNowOrganizationId().equals(shipment.getJcToOrganizationId())) {
            List<String> noUnloadLedIds = jcOrderBarcodeService.selectNoUnloadLedIdsByShipmentIdAndOrgId(shipmentId, shipment.getJcNowOrganizationId());
            if (noUnloadLedIds == null || noUnloadLedIds.isEmpty()) {
                // 这种情况说明该站点已经完成卸货
                this.updateRegTime(shipmentId, "org");
                num = 2;
            }
        }
        //再次查询所有是否有未卸货的分段订单
        ledIds = jcOrderBarcodeMapper.selectNoUnloadLedIdsByShipmentId(shipmentId);
        if (ledIds == null || ledIds.size() == 0) {
            // 完成整个运单的运输
            jcShipmentTrackService.addTrack(shipment.getId(), "运单已在目的站" + shipment.getToOrganization().getName() + "验收确认!");
            shipment.setStatus(ShipmentStatus.TRANSPORT_FINISHED.getValue());
            shipment.setFactArriveTime(new Date());
            jcShipmentMapper.updateById(shipment);
        }

        return num;
    }


    private JcAbnormalDetail getAbnormalDetail(String source, String sourceValue, String targer, String targerValue) {
        JcAbnormalDetail abnormalDetail = new JcAbnormalDetail();
        abnormalDetail.setSource(source);
        abnormalDetail.setTarger(targer);
        abnormalDetail.setSourceValue(sourceValue);
        abnormalDetail.setTargerValue(targerValue);
        return abnormalDetail;
    }


    /**
     * 批量删除运单管理
     *
     * @param ids 需要删除的运单管理主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentByIds(Long[] ids) {
        // 删除关联的台账明细
        List<JcShipment> shipments = jcShipmentMapper.selectBatchIds(Arrays.asList(ids));
        for (JcShipment shipment : shipments) {
            // ShipmentStatus
            if (shipment.getStatus() > 1) {
                throw new ServiceException("运单已经确认，不能删除");
            }
            // 删除关联的台账明细
            jcLedgerDetailService.deleteByShipmentId(shipment.getId());
            // jc_berth_stand
            jcBerthStandService.deleteJcBerthStandByShipmentId(shipment.getId());
            // jc_shipment_track
            jcShipmentTrackService.deleteJcShipmentTrackByShipmentId(shipment.getId());
        }
        return jcShipmentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除运单管理信息
     *
     * @param id 运单管理主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentById(Long id) {
        return jcShipmentMapper.deleteById(id);
    }

    @Override
    public int deleteJcShipmentByCode(String code) {
        return 0;
    }

    @Override
    public void printShipment(HttpServletResponse response, Long[] ids) {
        List<JcShipmentPrintVO> printList = new ArrayList<>();
        List<JcShipment> shipments = jcShipmentMapper.selectBatchIds(Arrays.asList(ids));
        for (JcShipment shipment : shipments) {
            JcShipmentPrintVO printVO = new JcShipmentPrintVO();
            printVO.setShipmentId(shipment.getId());
            printVO.setShipmentCode(shipment.getCode());
            printVO.setCodeImg(QrCodeUtils.getQrCode(shipment.getCode()));
            printVO.setTime(DateUtils.formatDate(shipment.getTime()));
            JcSysOrganization organization = sysOrganizationService.getById(shipment.getJcOrganizationId());
            printVO.setOrganizationName(organization.getName());
            JcSysOrganization fromOrganization = sysOrganizationService.getById(shipment.getJcFromOrganizationId());
            printVO.setFromOrganizationName(fromOrganization.getName());
            JcSysOrganization toOrganization = sysOrganizationService.getById(shipment.getJcToOrganizationId());
            printVO.setToOrganizationName(toOrganization.getName());
            printVO.setOutPhone(shipment.getOutPhone());
            printVO.setOutDriver(shipment.getOutDriver());
            printVO.setLicense(shipment.getLicense());
            // 装载的订单
            List<JcLed> leds = jcLedMapper.selectJcLedListByShipmentId(shipment.getId());
            List<JcLedPrintVO> ledPrintVOS = new ArrayList<>();
            for (JcLed led : leds) {
                JcLedPrintVO ledPrintVO = new JcLedPrintVO();
                ledPrintVO.setId(led.getId());
                ledPrintVO.setOrderId(led.getJcOrderId());
                ledPrintVO.setCode(led.getCode());
                ledPrintVO.setTime(DateUtils.formatDate(led.getTime()));
                ledPrintVO.setType(led.getType());
                ledPrintVO.setNumber(led.getNumber());
                ledPrintVO.setBackNumber(led.getBackNumber());
                JcLedReceivingParty receivingParty = jcLedReceivingPartyService.selectJcLedReceivingPartyListByLedIdAndType(led.getId(), "1");
                ledPrintVO.setParty(receivingParty);
                ledPrintVOS.add(ledPrintVO);
            }
            printVO.setLeds(ledPrintVOS);
            printList.add(printVO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("printList", printList);
        // 汇总信息-订单总数量
        int ledCount = 0;
        // 汇总信息-货品总数量
        int goodsCount = 0;
        for (JcShipmentPrintVO jcShipmentPrintVO : printList) {
            ledCount += jcShipmentPrintVO.getLeds().size();
            for (JcLedPrintVO led : jcShipmentPrintVO.getLeds()) {
                goodsCount += led.getNumber();
            }
        }
        map.put("ledCount", ledCount);
        map.put("goodsCount", goodsCount);
        PdfUtils pdfUtils = new PdfUtils();
        pdfUtils.exportPdfByte(response, "shipment", map);
    }

    @Override
    public void printSignPdf(HttpServletResponse response, Long[] ids) {
        List<Map<String, Object>> list = new ArrayList<>();
        //只能打印已经确认的运单
        List<JcShipment> jcShipments = listByIds(Arrays.asList(ids));
        for (JcShipment jcShipment : jcShipments) {
            if (jcShipment.getStatus() == ShipmentStatus.ACTIVE.getValue()) {
                throw new RuntimeException("只能打印已经确认后的运单");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("shipment", jcShipment);
            List<JcLedVO> leds = jcLedMapper.selectJcLedVOListByShipmentId(jcShipment.getId());
            map.put("leds", leds);
            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", list);
        PdfUtils pdfUtils = new PdfUtils();
        pdfUtils.exportPdfByte(response, "customer_sign", map);

    }

    @Override
    public String printSignBase64Pdf(Long[] ids) {
        List<Map<String, Object>> list = new ArrayList<>();
        //只能打印已经确认的运单
        List<JcShipment> jcShipments = listByIds(Arrays.asList(ids));
        for (JcShipment jcShipment : jcShipments) {
            if (jcShipment.getStatus() == ShipmentStatus.ACTIVE.getValue()) {
                throw new RuntimeException("只能打印已经确认后的运单");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("shipment", jcShipment);
            List<JcLedVO> leds = jcLedMapper.selectJcLedVOListByShipmentId(jcShipment.getId());
            map.put("leds", leds);
            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", list);
        PdfUtils pdfUtils = new PdfUtils();
        return pdfUtils.exportPdfFile("customer_sign", map);
    }

    @Override
    public Map<String, Object> printShipmentData(Long[] ids) {
        List<JcShipmentPrintVO> printList = new ArrayList<>();
        List<JcShipment> shipments = jcShipmentMapper.selectBatchIds(Arrays.asList(ids));
        for (JcShipment shipment : shipments) {
            JcShipmentPrintVO printVO = new JcShipmentPrintVO();
            printVO.setShipmentId(shipment.getId());
            printVO.setShipmentCode(shipment.getCode());
            printVO.setCodeImg(QrCodeUtils.getQrCode(shipment.getCode()));
            printVO.setTime(DateUtils.formatDate(shipment.getTime()));
            JcSysOrganization organization = sysOrganizationService.getById(shipment.getJcOrganizationId());
            printVO.setOrganizationName(organization.getName());
            JcSysOrganization fromOrganization = sysOrganizationService.getById(shipment.getJcFromOrganizationId());
            printVO.setFromOrganizationName(fromOrganization.getName());
            JcSysOrganization toOrganization = sysOrganizationService.getById(shipment.getJcToOrganizationId());
            printVO.setToOrganizationName(toOrganization.getName());
            printVO.setOutPhone(shipment.getOutPhone());
            printVO.setOutDriver(shipment.getOutDriver());
            printVO.setLicense(shipment.getLicense());
            // 装载的订单
            List<JcLed> leds = jcLedMapper.selectJcLedListByShipmentId(shipment.getId());
            List<JcLedPrintVO> ledPrintVOS = new ArrayList<>();
            for (JcLed led : leds) {
                JcLedPrintVO ledPrintVO = new JcLedPrintVO();
                ledPrintVO.setId(led.getId());
                ledPrintVO.setOrderId(led.getJcOrderId());
                ledPrintVO.setCode(led.getCode());
                ledPrintVO.setTime(DateUtils.formatDate(led.getTime()));
                ledPrintVO.setType(led.getType());
                ledPrintVO.setNumber(led.getNumber());
                ledPrintVO.setBackNumber(led.getBackNumber());
                JcLedReceivingParty receivingParty = jcLedReceivingPartyService.selectJcLedReceivingPartyListByLedIdAndType(led.getId(), "1");
                ledPrintVO.setParty(receivingParty);
                ledPrintVOS.add(ledPrintVO);
            }
            printVO.setLeds(ledPrintVOS);
            printList.add(printVO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("printList", printList);
        // 汇总信息-订单总数量
        int ledCount = 0;
        // 汇总信息-货品总数量
        int goodsCount = 0;
        for (JcShipmentPrintVO jcShipmentPrintVO : printList) {
            ledCount += jcShipmentPrintVO.getLeds().size();
            for (JcLedPrintVO led : jcShipmentPrintVO.getLeds()) {
                goodsCount += led.getNumber();
            }
        }
        map.put("ledCount", ledCount);
        map.put("goodsCount", goodsCount);
        return map;
    }
}
